<?php
namespace Rbac\Controller;
use Rbac\C\DataConverter;
use Rbac\C\Xpublic;
use Fn\App;
use Fn;
/**
  +----------------------------------------------------------
 * 课程管理控制器
  +----------------------------------------------------------
 * Description CurriculumController
  +----------------------------------------------------------
 * @author Jonas_yang 2016-2-14
  +----------------------------------------------------------
 */
class CurriculumController extends ExtendController{
    /**
     * 显示数据
     */
    public function index() {
        $modelName = $_GET["cmd"];
        $map = array();
        $filterFunction= "_filter".$modelName;
        if(method_exists($this,$filterFunction)){
            $map = $this->$filterFunction();
        }
        $model = D($modelName);
        $data = null;
        if (!empty($model)) {
            $data = $this->_list($model, $map);
        }

        if ($data["rows"] == null) {
            $data["rows"] = array();
        }
        $processFunction = "_process".$modelName;
        if(method_exists($this,$processFunction)){
            $data = $this->$processFunction($data);
        }
        //过滤数据
        $this->ajaxReturn($data);
    }
    
     /**
     * 数据搜索
     * @return type
     */
    protected function _filterMgCurriculum(){
        $model = M();
        $sql = "select * from mg_curriculum as mg,info_curriculum as cu where cu.cu_id = mg.cu_id";
        if(!empty($_GET["is_user"])&&$_GET["is_user"]=="my"){
            $sql = $sql." AND mg.it_id = ". App::getUserId();
        }
        if(!empty($_POST["cu_name"])){
            $sql = $sql." AND cu.cu_name like '".$_POST["cu_name"]."%'";
        }
        //排序方式，
        if (isset($_REQUEST["_sort"])) {
            $sort = $_REQUEST["_sort"] ? 'asc' : 'desc';
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        $page = $this->getLimitPage();
        $count = $model->query(str_replace("*", "count(*) as num", $sql));
        $sql = $sql." limit ".$page["offset"].",".$page["pagesize"];
        $row = $model->query($sql);
        $data["rows"] = empty($row)?array():$row;
        $data["total"] = $count[0]["num"];
        $this->ajaxReturn($data);
    }
    
     /**
     * 数据搜索
     * @return type
     */
    protected function _filterInfoCurriculum(){
        $map = array();
        if(!empty($_GET["is_user"])&&$_GET["is_user"]=="my"){
            $map["it_id"] = App::getUserId();
        }
        if(!empty($_POST["cu_name"])){
            $map["cu_name"] = array("like",trim($_POST["cu_name"])."%");
        }
        return $map;
    }
    
    
    protected function _processMgCurriculum($data){
        $infoCurriculumModel = M("info_curriculum");
        foreach($data["rows"] as $key=>$mgcurriculum){
            $map["cu_id"] = $mgcurriculum["cu_id"];
            $curriculum = $infoCurriculumModel->field("cu_name")->where($map)->find();
            $mgcurriculum["cu_name"] = $curriculum["cu_name"];
            $data["rows"][$key] = $mgcurriculum;
        }
        return $data;
    }

    /**
     * 课程信息
     */
    public function curriculum(){
        $this->display();
    }
    
    /**
     * 我的课程信息
     */
    public function myCurriculum(){
        $this->display();
    }
    
     /**
     * 专业课程信息
     */
    public function mgCurriculum(){
        $this->display();
    }
    
    /**
    *上传学生信息第一步
    **/
    public function uploadCurriculumStep1(){
        \Fn\App::setTimeLimit(3600);
         if (empty($_FILES)) {
            $this->ajaxReturn(array("success" => false, "message" => "上传数据为空"));
            exit;
        }
        $importData = new Xpublic\ImportData();
        //读取字段
        $dataFiled = DataConverter\DataConverterForCu::getField();
        $importData->setMatchFieldParam("name");
        if (!empty($_POST["splitFlag"])) {
            $splitFlag = I("post.splitFlag");
            if ($splitFlag === "t") {
                $splitFlag = "\t";
            }
        }
        //解析数据
        $tempFiledData = $importData->step1($_FILES, $dataFiled, $splitFlag);
        session("upload_erp_pre_orders_filePath", $tempFiledData["tempFilePath"]);
        $this->ajaxReturn(array(
            "success" => true,
            "rows" => $tempFiledData["data"],
            "sqlField" => $dataFiled
        ));
    }

     /**
     * 上传文件第二步
     */
    public function uploadCurriculumStep2() {
        \Fn\App::setTimeLimit(3600);
        $tempFilePath = session("upload_erp_pre_orders_filePath"); // 得到临时文件地址
        $field = $_POST ["field"]; // 获取提交表单中的字段映射关系
        $importData = new Xpublic\ImportData();
        $data = $importData->step2($tempFilePath, $field);
        $result = DataConverter\DataConverterForCu::importData($data);
        $response = array(
            "success" => true,
            "message" => "共成功添加" . $result . "条记录"
        );
        $this->ajaxReturn($response);
    }


    //按照班级选课
    public function xuankeByClass(){
        //获取到班级信息
        $iclIds =  $_POST["icl_id"];
        $iclIds = explode(",", $iclIds);
        $classModel = M("info_class");
        $mgCurriculumModel = M("mg_curriculum");
        $mc_year = $_POST["mc_year"];
        $it_id = $_POST["it_id"];
        $cu_id = $_POST["cu_id"];
        $mc_number = $_POST["mc_number"];
        foreach($iclIds as $id){
            //插入数据
            $classInfo = $classModel->find($id);  //获取班级信息
            //获取到数据
            $data["mc_year"] = $mc_year;
            $data["cu_id"] = $cu_id;
            $data["icl_id"] = $classInfo["icl_id"];
            $mgCurriculum = $mgCurriculumModel->where($data)->find();
            $data["mc_number"]  = $mc_number;
            $data["mc_grade"] = $classInfo["icl_year"];
            $data["id_id"] = $classInfo["id_id"];
            $data["ico_id"] = $classInfo["ico_id"];
            $data["it_id"] = $it_id;
            if(empty($mgCurriculum)){
                $mgCurriculumModel->add($data);
            }
        }
        $result["success"] = 1;
        $result["msg"] = "添加数据成功";
        $this->ajaxReturn($result);
         
    }
    
    //按照专业选课
    public function xuankeByMajor(){
        //获取到班级信息
        $idIds =  $_POST["id_id"];
        $idIds = explode(",", $idIds);
        $classModel = M("info_class");
        $mgCurriculumModel = M("mg_curriculum");
        $mc_year = $_POST["mc_year"];
        $it_id = $_POST["it_id"];
        $cu_id = $_POST["cu_id"];
        $mc_number = $_POST["mc_number"];
        foreach($idIds as $id){
            $iclIds = $classModel->where(array("id_id"=>$id))->select();
            foreach($iclIds as $classInfo){
                //获取到数据
                $data["mc_year"] = $mc_year;
                $data["cu_id"] = $cu_id;
                $data["icl_id"] = $classInfo["icl_id"];
                $mgCurriculum = $mgCurriculumModel->where($data)->find();
                $data["mc_number"]  = $mc_number;
                $data["mc_grade"] = $classInfo["icl_year"];
                $data["id_id"] = $classInfo["id_id"];
                $data["ico_id"] = $classInfo["ico_id"];
                $data["it_id"] = $it_id;
                if(empty($mgCurriculum)){
                    $mgCurriculumModel->add($data);
                }
            }

        }
        $result["success"] = 1;
        $result["msg"] = "添加数据成功";
        $this->ajaxReturn($result);
    }
}
